//
//  MeInfoView.m
//  
//
//  Created by 泽华伟业 on 17/3/15.
//
//

#import "MeInfoView.h"
#import "LoginViewController.h"

@interface MeInfoView ()

@property (nonatomic,strong) UILabel *titleLabel;
//用户名label
@property (nonatomic,strong) UILabel *userNameLabel;
//idlabel
@property (nonatomic,strong) UILabel *idLable;
//姓名
@property (nonatomic,strong) UILabel *nameLebel;
//用户类型
@property (nonatomic,strong) UILabel *typeLabel;
//取消按钮
@property (nonatomic,strong) UIButton *cancelButton;
//退出登录按钮
@property (nonatomic,strong) UIButton *logoutButton;
@end

@implementation MeInfoView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self titleLabel];
        self.backgroundColor = RGBCOLOR(231, 231, 231);
        [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [self.logoutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    }
    return self;
}

- (UILabel *)titleLabel{
    if (!_titleLabel ) {
        _titleLabel = [[UILabel alloc]init];
        [_titleLabel setTextColor:[UIColor whiteColor]];
        [_titleLabel setBackgroundColor:RGBCOLOR(116, 168, 229)];
        [_titleLabel setFont:[UIFont systemFontOfSize:19]];
        [_titleLabel setTextAlignment:NSTextAlignmentCenter];
        [_titleLabel setText:@"用户信息"];
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.height.equalTo(self).dividedBy(4);
        }];
    }
    return _titleLabel;
}

- (UILabel *)userNameLabel{
    if (!_userNameLabel) {
        _userNameLabel = [[UILabel alloc]init];
        [self addAndSetLabel:_userNameLabel];
        [_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(_titleLabel.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(200*wl, 25*hl));
        }];
    }
    return _userNameLabel;
}

- (UILabel *)idLable{
    if (!_idLable) {
        _idLable = [[UILabel alloc]init];
        [self addAndSetLabel:_idLable];
        [_idLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.centerX.equalTo(_userNameLabel);
            make.top.equalTo(_userNameLabel.mas_bottom);
        }];
    }
    return _idLable;
}

- (UILabel *)nameLebel{
    if (!_nameLebel) {
        _nameLebel = [[UILabel alloc]init];
        [self addAndSetLabel:_nameLebel];
        [_nameLebel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.centerX.equalTo(_idLable);
            make.top.equalTo(_idLable.mas_bottom);
        }];
    }
    return _nameLebel;
}

- (UILabel *)typeLabel{
    if (!_typeLabel) {
        _typeLabel = [[UILabel alloc]init];
        [self addAndSetLabel:_typeLabel];
        [_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.centerX.equalTo(_nameLebel);
            make.top.equalTo(_nameLebel.mas_bottom);
        }];
    }
    return _typeLabel;
}

- (UIButton *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc]init];
        [self addSubview:_cancelButton];
        [_cancelButton.titleLabel setFont:TEXT_FONT];
        _cancelButton.backgroundColor = [UIColor whiteColor];
        [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(cancelButtonDidSelected:) forControlEvents:UIControlEventTouchUpInside];
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(8*wl);
            make.bottom.equalTo(self.mas_bottom).offset(-8*hl);
            make.height.mas_equalTo(35*hl);
            make.width.equalTo(self).dividedBy(2.2);
        }];
    }
    return _cancelButton;
}

- (UIButton *)logoutButton{
    if (!_logoutButton) {
        _logoutButton = [[UIButton alloc]init];
        [self addSubview:_logoutButton];
        [_logoutButton.titleLabel setFont:TEXT_FONT];
        [_logoutButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _logoutButton.backgroundColor = [UIColor whiteColor];
        [_logoutButton addTarget:self action:@selector(logoutButtonDidSelected:) forControlEvents:UIControlEventTouchUpInside];
        [_logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_cancelButton.mas_right).offset(10*wl);
            make.top.equalTo(_cancelButton);
            make.height.width.equalTo(_cancelButton);
        }];
    }
    return _logoutButton;
}

- (void)addAndSetLabel:(UILabel *)label{
    [self addSubview:label];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setFont:TEXT_FONT];
}

//给各个label赋值
- (void)setTextForLabelsByUserName:(NSString *)userName UserId:(NSString *)userId Name:(NSString *)name Type:(NSString *)userType{
    [self.userNameLabel setText:[NSString stringWithFormat:@"用户名：%@",userName]];
    [self.idLable setText:[NSString stringWithFormat:@"用户ID：%@",userId]];
    [self.nameLebel setText:[NSString stringWithFormat:@"用户姓名：%@",name]];
    [self.typeLabel setText:[NSString stringWithFormat:@"用户类型：%@",userType]];
}

#pragma mark -- 按钮点击方法
- (void)cancelButtonDidSelected:(UIButton *)sender{
    //根据tag值找到半透明视图
    UIView *view = [self.window viewWithTag:170311];
    [view removeFromSuperview];
    [self removeFromSuperview]; 
}
///退出登录
- (void)logoutButtonDidSelected:(UIButton *)sender{
    [UIApplication sharedApplication].keyWindow.rootViewController = [[LoginViewController alloc]init];
    [USER_DEFAULT removeObjectForKey:@"userInfoModel"];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
